import type { Locales } from '../../locales/type'
import zh from '../../locales/zh'
import en from '../../locales/en'
import { useTagsViewStoreHook } from '@/stores/modules/tagsView'

const localStorageKey = 'simple-language'
const lang: 'zh' | 'en' = getLanguage()

const messages = {
  zh: {
    ...zh
  },
  en: {
    ...en
  }
}

export function setLanguage(lang: 'zh' | 'en') {
  localStorage.setItem(localStorageKey, lang)
  window.location.reload()
  useTagsViewStoreHook().refreshTagsView(lang === 'zh' ? 'en' : 'zh',lang,lang === 'zh' ? en : zh)

}

export function getLanguage() {
  return (localStorage.getItem(localStorageKey) as 'zh' | 'en') || 'zh'
}

export function transformI18n(key: keyof Locales,l?: 'zh' | 'en') {
  return messages[l || lang][key] || ''
}
